package com.dcbeida.activity.clerkmanager;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.addnewpatient.SexAct;
import com.dcbeida.bean.Customer;
import com.dcbeida.utils.Constants;
import com.dcbeida.utils.Utils;
import com.dcbeida.widget.TipDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import butterknife.Bind;

/**
 * 编辑和增加店员
 */
public class AddAndEditClerkActivity extends BaseActivity implements View.OnClickListener,
        AddAndEditClerkContract.View {

    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRright;
    @Bind(R.id.tv_save)
    TextView tvSave;
    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.tv_sex)
    TextView tvSex;
    @Bind(R.id.et_tel)
    EditText etTel;
    @Bind(R.id.tv_entry_date)
    TextView tvEntryDate;
    @Bind(R.id.et_office)
    EditText etOffice;
    @Bind(R.id.et_edu)
    EditText etEdu;
    @Bind(R.id.et_professional)
    EditText etProfessional;
    @Bind(R.id.et_job_title)
    EditText etJobTitle;


    private Customer mCustomer;
    private int type;//0是添加 1是编辑
    private AddAndEditClerkPresenter mPresent;
    private String orgTel;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_add_edit_clerk;
    }

    @Override
    public void setData() {
        super.setData();
        setListener();
        intentToData();

    }

    private void setListener() {
        imgBack.setOnClickListener(this);
        tvRright.setOnClickListener(this);
        tvSex.setOnClickListener(this);
        tvSave.setOnClickListener(this);
        tvEntryDate.setOnClickListener(this);
        tvRright.setVisibility(View.GONE);
        imgBack.setVisibility(View.VISIBLE);
        tvTitle.setVisibility(View.VISIBLE);
    }

    private void intentToData() {
        Bundle extras = getIntent().getExtras();
        type = extras.getInt("type");
        if (type == 0) {
            tvTitle.setText("添加店员");
        } else
            tvTitle.setText("编辑店员账号");
        if (extras.containsKey("customer")) {
            mCustomer = (Customer) extras.getSerializable("customer");
            tvSex.setText(mCustomer.getGender());
            etName.setText(mCustomer.getName());
            etTel.setText(mCustomer.getMobile());
            orgTel = mCustomer.getMobile();
            tvEntryDate.setText(mCustomer.getCheckintime());
            etEdu.setText(mCustomer.getDiploma());
            etProfessional.setText(mCustomer.getMajor());
            etOffice.setText(mCustomer.getJob());
            etProfessional.setText(mCustomer.getJobtitle());
            if(mCustomer.getStatus() ==1){
                tvSave.setText("提交");
            }
        }
        mPresent = new AddAndEditClerkPresenter(this, this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                closeInput(this);
                finish();
                break;
            case R.id.tv_sex:
                Bundle bundle = new Bundle();
                bundle.putString("sex", tvSex.getText().toString());
                bundle.putString("other", "1");
                showActivityForResult(SexAct.class, bundle, Constants.FOUR);
                break;
            case R.id.tv_right:
                break;
            case R.id.tv_entry_date:
                Calendar d = Calendar.getInstance(Locale.CHINA);
                Date myDate = new Date();
                d.setTime(myDate);
                int year = d.get(Calendar.YEAR);
                int month = d.get(Calendar.MONTH);
                int day = d.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                Log.i("fancl", "year:" + year + "//monthOfYear:" + monthOfYear + "//dayOfMonth:" + dayOfMonth);
                                Calendar cal = new GregorianCalendar(year, monthOfYear, dayOfMonth);
                                String pattern = "yyyy-MM-dd";
                                SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
                                String time2 = dateFormat.format(cal.getTime());
                                tvEntryDate.setText(time2);
                            }
                        }, year, month, day);
                datePickerDialog.show();
                break;
            case R.id.tv_save:
                if (mCustomer == null) {
                    mCustomer = new Customer();
                }
                if (etName.getText().toString().trim().length() == 0) {
                    showTip("请输入必选项");
                    return;
                }
                if (tvSex.getText().toString().trim().length() == 0) {
                    showTip("请选择必选项");
                    return;
                }
                if (etTel.getText().toString().trim().length() == 0) {
                    showTip("请输入必选项");
                    return;
                }
                if (tvEntryDate.getText().toString().trim().length() == 0) {
                    showTip("请选择必选项");
                    return;
                }
                mCustomer.setUid(MyApplication.user.getUid());
                mCustomer.setName(etName.getText().toString().trim());
                mCustomer.setGender(tvSex.getText().toString().trim());
                mCustomer.setMobile(etTel.getText().toString().trim());
                mCustomer.setCheckintime(tvEntryDate.getText().toString().trim());
                mCustomer.setJob(etOffice.getText().toString().trim());
                mCustomer.setDiploma(etEdu.getText().toString().trim());
                mCustomer.setMajor(etProfessional.getText().toString().trim());
                mCustomer.setJobtitle(etJobTitle.getText().toString().trim());
                if (type == 0) {
                    mPresent.requestToAdd(mCustomer);
                } else
                    mPresent.requestToModfiy(mCustomer);
                break;
        }
    }

    @Override
    public void setPresenter(AddAndEditClerkContract.Present presenter) {

    }

    @Override
    public void showTip(String s) {
        Utils.showTip(this, s);
    }

    @Override
    public void requestToSuccess() {
        MyApplication.getInstance().currentType = 1;
        Activity activity = MyApplication.getInstance().findActivity(ClerkStatueActivity.class);
        if (activity != null)
            activity.finish();
        if (type == 0) {
            TipDialog tipDialog = new TipDialog(mContext).setMessage("帐号提交成功，审核通过后即可使用")
                    .setTxColor(R.color.c_333333);
            tipDialog.setListener(new TipDialog.IKonowListener() {
                @Override
                public void iKown() {
                    finish();
                }
            });
            tipDialog.show();
        }else {
            if(mCustomer.getStatus() == 1){//启用状态下
                if(mCustomer.getMobile().equals(orgTel)){//没改手机号码
                    MyApplication.getInstance().currentType= 0;
                    showTip("修改成功");
                    finish();
                }else { //改了手机号码
                    TipDialog tipDialog = new TipDialog(mContext).setMessage("帐号提交成功，审核通过后即可使用")
                            .setTxColor(R.color.c_333333);
                    tipDialog.setListener(new TipDialog.IKonowListener() {
                        @Override
                        public void iKown() {
                            finish();
                        }
                    });
                    tipDialog.show();
                }
            }else { //未启用
                TipDialog tipDialog = new TipDialog(mContext).setMessage("帐号提交成功，审核通过后即可使用")
                        .setTxColor(R.color.c_333333);
                tipDialog.setListener(new TipDialog.IKonowListener() {
                    @Override
                    public void iKown() {
                        finish();
                    }
                });
                tipDialog.show();
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.FOUR && resultCode == RESULT_OK) {//选择医院
            Bundle bundle = data.getExtras();
            if (null != bundle) {
                if (bundle.containsKey("sex")) {
                    tvSex.setText(bundle.getString("sex"));
                }
            }

        }
    }
}
